
<?php if (!$this->isSuccess):?>

<style type="text/css">
<!--
#add_question_form div.form-label
{
  float: none;
  text-align: left;
  margin: 0px;
  padding: 0px 0px 5px 0px;
}
html[dir="rtl"] #add_question_form div.form-label
{
  text-align: right;
}
#add_question_form div.form-element
{
  float: none;
}
.answer_correct{
	height: 25px;
}
.answer_correct input{
	margin-top: 2px;
}
-->
</style>
<script type="text/javascript">
var index_answer_next = <?php echo count($this->answers) + 1?>;
var defaultValue = '<?php echo $this->translate('Add answer...')?>';
function cleartext(field){
	if(defaultValue == field.value)
		field.value = '';
	else if(field.value == '')		
		field.value = defaultValue;
};

function addAnswerMore(){			
	$answer = new Element('input',{ 
        'class' : 'answer',
        type : 'text', 
        name: 'answer[]',  
        value : '<?php echo $this->translate('Add answer...');?>',
        defaultvalue: '<?php echo $this->translate('Add answer...');?>',
        onfocus: 'cleartext(this)',
        onblur: 'cleartext(this)'        
		}).inject($('answer-element').getElements('input').getLast(),'after');
	
	return $answer;
};
function addIsCorrectMore(){
	$div = new Element('div', {
		'class' : 'answer_correct'
	}).inject($('correct-element').getElements('.answer_correct').getLast(),'after');
	$correct = new Element('input',{
		 'class' : 'answer_correct_input',
		 type : 'checkbox',		
		 name : 'answer_correct_' + index_answer_next
		}).inject($div);
}
function updateAnswer(id, obj){
    var value = obj.value;
    var request = new Request.JSON({
        'url' : '<?php echo $this->url(array('controller' => 'ajax', 'action' => 'edit-answer'), '', true);?>',
        'data' : {
            'answer_id' : id,
            'content' : value
        },
        'format': 'json',
        onSuccess : function(responseJSON) {
            parent.loadQuestions('<?php echo $this->exam->getIdentity() ?>');
        }
    });

    request.send();
}
function check(id, obj) {
        var value = (obj.checked == true) ? 1 : 0;
        (new Request.JSON({
            'format': 'json',
            'url' : '<?php echo $this->url(array('controller' => 'ajax', 'action' => 'edit-answer-check'), '', true);?>',
            'data' : {
                'format' : 'json',
                'answer_id' : id,
                'value' : value
            },
            'onSuccess' : function(responseJSON, responseText)
            {
                parent.loadQuestions('<?php echo $this->exam->getIdentity() ?>');
            }
        })).send();

    }
window.addEvent('domready', function(){
	
	$('answer_more').addEvents({
		'click': function(e){
			cleartext(this);
			e.stop();
			$answer_last_value =  $('answer-element').getElements('input').getLast().get('value');
			if($answer_last_value != '<?php echo $this->translate('Add answer...');?>' && $answer_last_value != ''){
				$answer = addAnswerMore();
				$correct = addIsCorrectMore();
				$answer.getParent().getChildren('input').getLast().focus();
				index_answer_next++;	
			}
		}									
	});

	$$('.answer').addEvents({
		'focus': function(){
			cleartext(this);
			},			
		'blur': function(){
			cleartext(this);
		}									
		});
       $('add_question_form').addEvent('submit', function(e){
           return false;
       });
});	
</script>
<form id="<?php echo $this->form->getAttrib('id');?>" name="<?php echo $this->form->getAttrib('name');?>" enctype="multipart/form-data" action="<?php echo $this->escape($this->form->getAction()) ?>" method="<?php echo $this->escape($this->form->getMethod()) ?>" class="global_form">
    <div>
        <div>
            <h3>
                <?php echo $this->translate($this->form->getTitle()) ?>
            </h3>
            <p class="form-description"><?php echo $this->translate($this->form->getDescription()) ?></p>
            <div class="form-elements">
            	<?php echo $this->form->category;?>
            	<?php echo $this->form->content; ?>
            	<?php //echo $this->form->answer; ?>
            	<div class="form-wrapper" id="answer-wrapper">
            	  <div class="answer_left" style="width: 225px;float: left;">
            		<div class="form-label" id="answer-label">
            			<label class="optional" ><?php echo $this->translate('Answer')?></label>
            		</div>
					<div class="form-element" id="answer-element">
                                                <?php foreach($this->answers as $answer):?>
                                            <input type="text" class="answer" value="<?php echo $answer->content?>" name="" onchange="updateAnswer(<?php echo $answer->getIdentity()?>, this)">
                                                <?php endforeach;?>
						<input type="text" class="answer" value="<?php echo $this->translate('Add answer...')?>" name="answer[]">
					</div>
				  </div>
				  <div class="answer_correct" style="float: left;">
				  	<div class="form-label">
            			<label class="optional" ><?php echo $this->translate('Is Correct')?></label>
            		</div>
					<div class="form-element" id="correct-element">
                                             <?php $i=0; foreach($this->answers as $answer):?>
                                                <div class="answer_correct">
                                                    <input class="answer_correct_input" <?php echo ($answer->result)?'checked':''?> type="checkbox"  onclick="check('<?php echo $answer->getIdentity()?>', this)" name="">
						</div>
                                             <?php $i++; endforeach;?>
						<div class="answer_correct">
                                                    <input class="answer_correct_input" type="checkbox" name="answer_correct_<?php echo $i?>">
						</div>
					</div>
				  </div>
				</div>
						           	
            	
            	<div class="form-wrapper" >
            		<div class="form-label" ></div>
					<div class="form-element" >
						<a style="cursor: pointer" class="buttonlink icon_more_answer" id="answer_more"><?php echo $this->translate('Add More')?></a>
					</div>
				</div>
				
				<?php echo $this->form->buttons; ?>
			</div>
		</div>
	</div>
</form>

<?php else:?>
<script type="text/javascript">
    //load ajax : addressedit
    parent.loadQuestions('<?php echo $this->exam->getIdentity() ?>');
    setTimeout(function()
    {
        parent.Smoothbox.close();
    }, 2000);
</script>
<div class="global_form_popup_message" style="padding: 5px;">
        <?php echo $this->translate('This question was created.'); ?>
</div>
<?php endif;?>